package my.json.tool;

import net.sf.cglib.reflect.FastClass;

import java.util.Map;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-10-12
 * Time: 17:53:58
 * To change this template use File | Settings | File Templates.
 */
public class ReflectFactory {
    private static ReflectFactory instance=new ReflectFactory();
    private Map<String, FastClass> classMap=new HashMap<String,FastClass>();
    private ReflectFactory(){}
    public static ReflectFactory getInstance(){
        return instance;
    }
    public FastClass getClazz(String type) throws Exception {
        FastClass fastClass=classMap.get(type);
        if(fastClass==null){
            Class clazz=Class.forName(type);
            fastClass=FastClass.create(clazz);
            classMap.put(type,fastClass);
        }
        return fastClass;
    }
    public Object newInstance(String type) throws Exception {
        return getClazz(type).newInstance();
    }

    public <T> T newInstance(String type,Class<T> t) throws Exception {
        return (T)newInstance(type);
    }
}
